/**
 * HR 月评
 */
import React from 'react';
import { useCollapse } from '@/hooks';
import { Collapse } from '@/molecules';
import { Form } from 'antd';
import {
    BottomAction,
    AssessAbilityResult,
    Attendance,
    AssessHR,
    DiscussArrange,
    AssessResultDynamic,
    AssessKPI
} from '../containers';
import { useAssessSetting } from '../hooks';

const { Panel } = Collapse;

function AssessHRM({ form, data = {} }) {
    const { assessMonthly = [], interviewJoinCodes = [] } = data;

    const { activeKey, onChange, openAll } = useCollapse(['1', '2', '3', '4', '5', '6'], []);
    const { m = {} } = useAssessSetting(data);

    const showDiscuss = React.useMemo(() => {
        const { evaluationType } = form.getFieldsValue();
        return ['0', '2'].includes(evaluationType);
    }, [form]);

    const params = React.useMemo(() => {
        const {
            resourceRelation = {},
            assessSummary = 0,
            project,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewDate,
            interviewStartTime,
            interviewEndTime,
            interviewType = 3,
            schedule,
            ...values
        } = form.getFieldsValue();
        const interviewData = {
            interviewType,
            interviewWay,
            interviewLocation,
            interviewEmployeeCodeList,
            interviewStartTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewStartTime?.format('HH:mm:00')}`,
            interviewEndTime: `${interviewDate?.format('YYYY-MM-DD')} ${interviewEndTime?.format('HH:mm:00')}`,
            schedule
        };
        const interviewArrange = showDiscuss ? { interviewArrange: interviewData } : {};
        const { uploadResourceFile } = resourceRelation;
        return {
            assess: {
                resourceRelation: uploadResourceFile ? [uploadResourceFile] : [],
                assessAbility: {
                    project
                },
                assessSummary: assessSummary[0],
                ...interviewArrange,
                ...values
            }
        };
    }, [form]);

    return (
        <div>
            <Collapse bordered={false} activeKey={activeKey} onChange={onChange}>
                <AssessResultDynamic data={data} />
                {m.showKPI && Boolean(data?.performanceTarget?.result) && (
                    <Panel header="季度绩效目标" key="2">
                        <AssessKPI data={data} />
                    </Panel>
                )}
                {m.showAssess && (
                    <Panel header="能力评估" key="3">
                        <AssessAbilityResult data={assessMonthly} levels={data?.assessAbility?.levels} />
                    </Panel>
                )}
                <Panel header="考勤情况" key="4">
                    <Attendance data={data} type="month" />
                </Panel>
                <Panel header="人事评估意见" key="5">
                    <AssessHR form={form} />
                </Panel>
                {showDiscuss && (
                    <Panel header="新员工面谈安排" key="6">
                        <DiscussArrange
                            data={{ interviewEmployeeCodeList: interviewJoinCodes, interviewWay: 1 }}
                            form={form}
                            interviewType={3}
                        />
                    </Panel>
                )}
            </Collapse>

            <BottomAction openAll={openAll} data={data} params={params} form={form} />
        </div>
    );
}

export default Form.create({})(AssessHRM);
